import store from 'src/utils/storage';
import fetch from "src/utils/fetch";
import API from "src/utils/api";
import notification from "src/utils/notification";

const Main = {
  state: {
    loading: false,
    regionLoading: false,
    // 每个页面的权限菜单
    pagePermissionsMenu:{},
    // 登录用户菜单
    loginUserMenus:[],
  },
  mutations: {
    //开启全局遮罩
    LOADING(state) {
      state.loading = true;
    },
    //关闭全局遮罩
    UNLOADING(state) {
      state.loading = false;
    },
    //开启列表局部遮罩
    REGION_LOADING(state) {
      state.regionLoading = true;
    },
    //关闭列表局部遮罩
    UN_REGION_LOADING(state) {
      state.regionLoading = false;
    },
    QUERY_ShowMy_MENUS(state, value) {
      let permissions={};
      state.loginUserMenus = value;
    },
    QUERY_PageBtn_MENUS(state, value) {
      state.pagePermissionsMenu=getPagePermissionMenus(value.allMenus,value.loginMenus);
      store.set('pagePermissionsMenu',state.pagePermissionsMenu);
    },
  },
  actions: {

      //请求登录用户菜单
    queryLoginUserMenus: ({ state, commit }, params) => {

        fetch({
          commit: commit,
          url: API.baseInfo.permissionMgr.showMyMenu,
          type: "post",
          data: params,
          showRegionLoading: true,
          success: function(data) {

            commit("QUERY_ShowMy_MENUS", data);
          },
          error: function(res) {
            notification.error(res.returnMsg);
          }
        });


    },
    //请求页面按钮权限
    queryPageBtnMenus: ({ state, commit }, params) => {

      new Promise((rs,rj)=>{
         fetch({
          commit: commit,
          url: API.baseInfo.permissionMgr.menuAll,
          type: "post",
          data: {},
          showRegionLoading: true,
          success: function(data) {
            rs(data);
          },
          error: function(res) {
            rj(res.returnMsg);
          }
        });
      }).then((allMenus)=>{

        fetch({
          commit: commit,
          url: API.baseInfo.permissionMgr.showMyMenu,
          type: "post",
          data: params,
          showRegionLoading: true,
          success: function(data) {

            let menus={
              allMenus : allMenus,
              loginMenus : data,
            };

            commit("QUERY_PageBtn_MENUS", menus);
          },
          error: function(res) {
            notification.error(res.returnMsg);
          }
        });

      })
    },
  },
  getters: {
    regionLoading(state, rootState) {
      return state.regionLoading;
    },
    pagePermissionsMenu(state, rootState) {
      return state.pagePermissionsMenu;
    },

    loginUserMenus(state, rootState) {
      return state.loginUserMenus;
    },

    loading(state, rootState) {
      return state.loading;
    },
  }
};

export default Main;

/**
 * [解析权限菜单]
 * @param  {[array]} arr  [后台返回原始数据]
 * @param  {[object]} obj [权限菜单对象]
 * @return {[object]}     [description]
 */
function parsePermissionMenus(arr, obj) {

  arr.forEach((item, index) => {

    // 有权限字段就进行解析
    if(item.permissions && item.permissions.length > 0){

      obj[item.code]={};
      let tempObj={};
      item.permissions && item.permissions.forEach((item,index)=>{
        tempObj[item.optCode]={canUse:0,id:item.optId,optDesc: item.optDesc};
      })
      obj[item.code]=tempObj;
    }

    // 有子菜单继续遍历
    if (item.children && item.children.length > 0) {
       parsePermissionMenus(item.children, obj);
    }

  })
}

/**
 * [解析页面案件权限]
 * @param  {[object}  allObj   [全部菜单]
 * @param  {[object]} loginObj [用户菜单]
 * @return {[type]}          [description]
 */
function parsePageBtnPermission(allObj,loginObj){

  let loginMenuArr=Object.keys(loginObj);

  for(let name in allObj) {

    if(Object.prototype.hasOwnProperty.call(allObj,name)){
         // 页面匹配
        if(loginMenuArr.indexOf(name) != -1){
           // 页面按键名称匹配
          if(Object.prototype.toString.call(allObj[name]) == "[object Object]"){
            parsePageBtnPermission(allObj[name],loginObj[name]);
          }else{
              name == 'canUse' ? allObj[name]=1 : '';
          }
        }
    }
  }
}

/**
 * [获取用户每个页面的权限]
 * @param  {[array}  allMenus   [全部菜单权限]
 * @param  {[array]} loginMenus [登录用户权限]
 * @return {[object]}            [用户页面权限]
 */
function getPagePermissionMenus(allMenus,loginMenus){
  let allObj={},loginObj={},allMenuArr=[],loginMenuArr=[],btnArr;
  parsePermissionMenus(allMenus,allObj);
  parsePermissionMenus(loginMenus,loginObj);
  parsePageBtnPermission(allObj,loginObj);
  return allObj;
}


